package com.kingdee.eas.custom.gz.utils;

import com.kingdee.bos.ctrl.kdf.table.*;
import com.kingdee.bos.ctrl.kdf.table.event.KDTMouseEvent;

import java.awt.event.MouseEvent;
import java.text.Collator;
import java.util.*;

/**
 * 
 * @author yangyihao. Just call me House.
 * @create 2008-1-23
 */
class HouseKDTSortManager extends KDTSortManager
{
    class TableCellComparator
        implements Comparator
    {

        public int compare(Object obj, Object obj1)
        {
            int i = doCompare(obj, obj1);
            if(sortType == 0)
                return i;
            if(i < 0)
                return 1;
            if(i > 0)
                return -1;
            else
                return i;
        }

        public int doCompare(Object obj, Object obj1)
        {
            Object obj2 = null;
            Object obj3 = null;
            KDTCell kdtcell = ((KDTRow)obj).getCell(colIndex);
            if(kdtcell != null)
                obj2 = kdtcell.getValue();
            kdtcell = ((KDTRow)obj1).getCell(colIndex);
            if(kdtcell != null)
                obj3 = kdtcell.getValue();
            if(obj2 == obj3)
                return 0;
            if(obj2 == null && obj3 != null)
                return -1;
            if(obj2 != null && obj3 == null)
                return 1;
            if(obj2 instanceof Number)
                if(obj3 instanceof Number)
                {
                    double d = ((Number)obj2).doubleValue();
                    double d1 = ((Number)obj3).doubleValue();
                    if(d == d1)
                        return 0;
                    else
                        return d < d1 ? -1 : 1;
                } else
                {
                    return obj2.toString().compareTo(obj3.toString());
                }
            if(obj2 instanceof String)
                if(obj3 instanceof String)
                    return comparator.compare(obj2, obj3);
                else
                    return ((String)obj2).compareTo(obj3.toString());
            if(obj2 instanceof Date)
                if(obj3 instanceof Date)
                    return ((Date)obj2).compareTo((Date)obj3);
                else
                    return obj2.toString().compareTo(obj3.toString());
            if(obj2 instanceof Calendar)
            {
                if(obj3 instanceof Calendar)
                {
                    long l = ((Calendar)obj2).getTimeInMillis();
                    long l1 = ((Calendar)obj3).getTimeInMillis();
                    if(l == l1)
                        return 0;
                    else
                        return l < l1 ? -1 : 1;
                } else
                {
                    return obj2.toString().compareTo(obj3.toString());
                }
            } else
            {
                return obj2.toString().compareTo(obj3.toString());
            }
        }

        int colIndex;
        int sortType;
        Collator comparator;

        public TableCellComparator(int i, int j)
        {
            colIndex = 0;
            comparator = Collator.getInstance(Locale.getDefault());
            colIndex = i;
            sortType = j;
        }
    }


    public HouseKDTSortManager(KDTable table)
    {
        super(table);
        isIncludeTotalRow = false;
        totalRowCount = -1;
    }

    public HouseKDTSortManager(KDTable table, boolean isIncludeTotalRow)
    {
        super(table);
        this.isIncludeTotalRow = false;
        totalRowCount = -1;
        this.isIncludeTotalRow = isIncludeTotalRow;
        if(isIncludeTotalRow)
            totalRowCount = 1;
    }

    public HouseKDTSortManager(KDTable table, int totalRowCount)
    {
        super(table);
        isIncludeTotalRow = false;
        this.totalRowCount = -1;
        isIncludeTotalRow = true;
        this.totalRowCount = totalRowCount;
    }

    public void sort(int colIndex, int sortType)
    {
        super.sort(colIndex, sortType);
        table.setRefresh(false);
        List list = table.getBody().getRows();
        List totalRowList = null;
        if(isIncludeTotalRow)
        {
            totalRowList = new ArrayList(totalRowCount);
            for(int i = totalRowCount; i > 0; i--)
            {
                totalRowList.add(((KDTRow)list.get(list.size() - 1)).clone());
                list.remove(list.size() - 1);
            }

        }
        Collections.sort(list, new TableCellComparator(colIndex, sortType));
        List listClone = new ArrayList(list.size());
        for(int i = 0; i < list.size(); i++)
        {
            KDTRow row = (KDTRow)list.get(0);
            listClone.add(row.clone());
            table.removeRow(0);
        }

        for(int i = 0; i < listClone.size(); i++)
            table.addRow(i, (KDTRow)listClone.get(i));

        if(totalRowList != null)
        {
            for(int i = totalRowList.size() - 1; i >= 0; i--)
                table.addRow(table.getRowCount(), (KDTRow)totalRowList.get(i));

        }
        table.setRefresh(true);
    }

    public int getTotalRowCount()
    {
        return totalRowCount;
    }

    public void setTotalRowCount(int totalRowCount)
    {
        this.totalRowCount = totalRowCount;
    }

    public void tableClicked(KDTMouseEvent e)
    {
        super.tableClicked(e);
        if(e.getClickCount() == 2 && e.getType() == MouseEvent.MOUSE_CLICKED)
            sort(e.getColIndex());
    }

    public boolean isIncludeTotalRow()
    {
        return isIncludeTotalRow;
    }

    public void setIncludeTotalRow(boolean isIncludeTotalRow)
    {
        this.isIncludeTotalRow = isIncludeTotalRow;
    }

    private boolean isIncludeTotalRow;
    private int totalRowCount;
}